﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Entities;
using NIIT.SaoBacDau.Aupi.HospitalManagement.Languages;
using NIIT.SaoBacDau.Aupi.HospitalManagement.BusinessLogic;

namespace NIIT.SaoBacDau.Aupi.HospitalManagement
{
    public partial class Account : Form
    {
        public Account(int option)
        {
            InitializeComponent();
            this.Option = option;
        }

        private int Option;

        private HR_Employee hR_Employee;

        private D_OrganizationDetails d_OrgDetails;

        private D_Positions d_Position;

        private HR_TypeEmployee hR_TypeEmployee;

        private HR_TypeDoctor hR_TypeDoctor;

        private int ManagementAccount;

        public Account(HR_Employee hr_Employee,D_OrganizationDetails d_OrgDetails,D_Positions d_Pos,HR_TypeEmployee hr_TypeE,HR_TypeDoctor hr_TypeDoc,int Member)
        {
            InitializeComponent();
            this.hR_Employee = hr_Employee;
            this.d_OrgDetails = d_OrgDetails;
            this.d_Position = d_Pos;
            this.hR_TypeEmployee = hr_TypeE;
            this.hR_TypeDoctor = hr_TypeDoc;
            this.ManagementAccount = Member;
        }

        private void Account_Load(object sender, EventArgs e)
        {
            if (this.Option == 0)
            {
                Department();
                btn_AddEmployee.Enabled = false;
                LoadEmployee();
                
            }
            else if (this.Option == 1)
            {
                Department();
                btn_SaveEmployee.Enabled = false;
            }
            if (ManagementAccount == 0)
                Disable();
        }

        private void LoadEmployee()
        {
            if (this.hR_Employee != null)
            {
                tbx_EmployeeID.Text = hR_Employee.EmployeeID.ToString();
                tbx_Name.Text = hR_Employee.NameEmployee;
                if (hR_Employee.Gender == true)
                    rbtn_Male.Checked = true;
                else
                    rbtn_Women.Checked = true;
                tbx_Address.Text = hR_Employee.Address;
                dtp_BirthDay.Text = hR_Employee.BirthDay.ToString();
                tbx_PhoneNumber.Text = hR_Employee.PhoneNumber;
                tbx_Identification.Text = hR_Employee.Identification.ToString();
                tbx_AddressEmail.Text = hR_Employee.Email;
                dtp_StartDate.Text = hR_Employee.StartDate.ToString();
                nud_Salary.Value = Convert.ToInt32(hR_Employee.Salary.ToString());
                cbx_PositionsName.Items.Add(d_Position.NamePositions.ToString());
                cbx_OrganizationDetailsName.Text = d_OrgDetails.NameDetails;
                cbx_TypeEmployee.Items.Add(hR_TypeEmployee.NameTypeEmployee + " " + hR_TypeDoctor.NameTypeDoctor);
                cbx_PositionsName.SelectedIndex = 0;
                cbx_TypeEmployee.SelectedIndex = 0;
            }
        }

        private void Disable()
        {
            tbx_Name.ReadOnly = true;
            rbtn_Male.Enabled = false;
            rbtn_Women.Enabled = false;
            dtp_BirthDay.Enabled = false;
            tbx_Identification.ReadOnly = true;
            dtp_StartDate.Enabled = false;
            nud_Salary.Enabled = false;
            cbx_TypeEmployee.Enabled = false;
            cbx_PositionsName.Enabled = false;
            cbx_OrganizationDetailsName.Enabled =false;
        }

        private void bt_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btn_SaveEmployee_Click(object sender, EventArgs e)
        {
            if (LibraryLanguages.UpdateEmployee() == true)
                UpdateEmployee();
        }

        private void Department()
        {
            cbx_OrganizationDetailsName.Items.Clear();
            List<D_OrganizationDetails> ListOrgDetails = BL_D_OrganizationDetailss.ListOrgDetails();
            for (int i = 0; i < ListOrgDetails.Count; i++)
            {
                cbx_OrganizationDetailsName.Items.Add(ListOrgDetails[i].NameDetails.ToString());
            }
            cbx_OrganizationDetailsName.SelectedIndex = 0;
        }

        private void UpdateEmployee()
        {
            HR_Employee hr_Em = new HR_Employee();
            if (ManagementAccount == 0)
            {
                hr_Em.EmployeeID = Convert.ToInt32(tbx_EmployeeID.Text);
                hr_Em.Address = tbx_Address.Text;
                hr_Em.PhoneNumber = tbx_PhoneNumber.Text;
                hr_Em.Email = tbx_AddressEmail.Text;
                CheckEmployee(hr_Em);
            }
            else
            {

            }
            
        }

        private void CheckEmployee(HR_Employee hr_Em)
        {
            int error = BL_HR_Employee.Update(hr_Em);
            switch(error)
            {
                case 1:
                    LibraryLanguages.UpdateProfileCompleate();
                    this.Dispose();
                    break;
                case 2:
                    LibraryLanguages.EPhoneNotFormat();
                    break;
                case 3:
                    LibraryLanguages.EmailNotFormat();
                    break;
                case 4:
                    LibraryLanguages.SqlError();
                    break;
                default:
                    break;
            }
        }

        private void btn_AddEmployee_Click(object sender, EventArgs e)
        {

        }
    }
}
